
package org.jeecg.modules.demo.xxljob;

import com.alibaba.fastjson.JSON;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.CommonAPI;
import org.jeecg.common.config.mqtoken.UserTokenContext;
import org.jeecg.common.constant.CommonConstant;
import org.jeecg.common.system.util.JwtUtil;
import org.jeecg.common.util.RedisUtil;
import org.jeecg.common.util.SpringContextUtils;
import org.jeecg.modules.demo.ch.common.CommonMst;
import org.jeecg.modules.message.handle.impl.EmailSendMsgHandle;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;

import java.util.Map;


/**
 * xxl-job定时任务测试
 */
@Component
@Slf4j
public class TestJobHandler {

    @Lazy
    @Autowired
    private CommonAPI commonApi;

    @Autowired
    private EmailSendMsgHandle emailSendMsgHandle;

    /**
     * 简单任务
     *
     * 测试：无token调用feign接口
     *
     * @param params
     * @return
     */
    @XxlJob(value = "testJob")
    public ReturnT<String> demoJobHandler(String params) {
        String jobParam = XxlJobHelper.getJobParam();
        Map<String,Object> map = (Map<String,Object>)JSON.parse(jobParam);
        String beginTime = (String)map.get("beginTime");
        String endTime = (String)map.get("endTime");

        XxlJobHelper.log("jeecgBoot自定义的testJob传入的beginTime{},endTime{}",beginTime,endTime);
        //1.生成临时令牌Token到线程中
        UserTokenContext.setToken(getTemporaryToken());

        log.info("我是 jeecg-demo 服务里的定时任务 testJob , 我执行了...............................");
        log.info("我调用 jeecg-system 获取IT部人员信息接口：{}",commonApi.getUserByDeptId(CommonMst.SOFTWARE.IT_DEPT_ID));
        //。。。此处可以写多个feign接口调用

        //测试发送邮箱
//        String es_receiver="490032898@qq.com";
//        String es_receiver="17695632435@163.com";
//        String es_receiver="it3@sz-changhong.com";
//        String es_receiver="it4@sz-changhong.com";
        String es_receiver="guyu@sz-changhong.com";
        String es_title="jeecg测试邮件";
        String es_content="测试内容";
        emailSendMsgHandle.sendMsg(es_receiver ,es_title ,es_content );



        //2.使用完，删除临时令牌Token
        UserTokenContext.remove();
        return ReturnT.SUCCESS;
    }

    public void init() {
        log.info("init");
    }

    public void destroy() {
        log.info("destory");
    }

    /**
     * 获取临时令牌
     *
     * 模拟登陆接口，获取模拟 Token
     * @return
     */
    public static String getTemporaryToken() {
        RedisUtil redisUtil = SpringContextUtils.getBean(RedisUtil.class);
        // 模拟登录生成Token
        String token = JwtUtil.sign("??", "??");
        // 设置Token缓存有效时间为 5 分钟
        redisUtil.set(CommonConstant.PREFIX_USER_TOKEN + token, token);
        redisUtil.expire(CommonConstant.PREFIX_USER_TOKEN + token, 5 * 60 * 1000);
        return token;
    }

}

